package com.wdd.myplatform.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.text.SimpleDateFormat;
import java.util.Collection;

@Slf4j
public class TablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<String>{
    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        try {
            String tableName = preciseShardingValue.getLogicTableName();
            log.info("tableName--------------{}",tableName);
            String dataTime = String.valueOf(preciseShardingValue.getValue());
            String year = dataTime.substring(0, 4);
            System.out.println("-------------------------"+year);
            String key = dataTime.substring(5, 7);
            System.out.println("-------------------------"+key);
            return tableName.concat(year).concat(key);
        }catch (Exception e){
            throw new IllegalArgumentException("没有匹配到库:" + preciseShardingValue.getValue());
        }
    }
}
